import { LoggerFactory } from '../src/utils/logger';
import * as dotenv from 'dotenv';

// Load environment variables from .env file FIRST
dotenv.config();

// Set up silent logging for tests
LoggerFactory.setDefaultType('silent');

// Global test timeout
jest.setTimeout(60000); // 增加到 60 秒

// Set test environment (but don't override RabbitMQ config from .env)
process.env.NODE_ENV = 'test';

// Only set default values if not already set by .env
if (!process.env.RABBITMQ_HOST) {
  process.env.RABBITMQ_HOST = 'localhost';
}
if (!process.env.RABBITMQ_PORT) {
  process.env.RABBITMQ_PORT = '5672';
}
if (!process.env.RABBITMQ_USERNAME) {
  process.env.RABBITMQ_USERNAME = 'guest';
}
if (!process.env.RABBITMQ_PASSWORD) {
  process.env.RABBITMQ_PASSWORD = 'guest';
}

// Global test utilities
global.sleep = (ms: number): Promise<void> => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

// Extend Jest matchers
declare global {
  function sleep(ms: number): Promise<void>;
}

// 强制进程清理
process.on('exit', () => {
  console.log('🧹 Process exiting, forcing cleanup...');
});

process.on('SIGINT', () => {
  console.log('🧹 SIGINT received, forcing cleanup...');
  process.exit(0);
});

process.on('SIGTERM', () => {
  console.log('🧹 SIGTERM received, forcing cleanup...');
  process.exit(0);
});
